package com.google.android.exoplayer2.extractor.ts;

import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.extractor.ts.TsPayloadReader.b;
import com.google.android.exoplayer2.extractor.ts.TsPayloadReader.d;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import o.adv;
import o.adw;
import o.ady;
import o.adz;
import o.aea.e;
import o.ago;
import o.agq;
import o.agr;
import o.ags;
import o.agv;
import o.aha;
import o.amm;
import o.anc;
import o.ane;
import o.anq;
import o.anr;

public final class TsExtractor implements Extractor {
    private static final long ˊ = ((long) anr.ᐝ("AC-3"));
    private static final long ˋ = ((long) anr.ᐝ("HEVC"));
    public static final adw ˏ = agr.ॱ;
    private static final long ॱ = ((long) anr.ᐝ("EAC3"));
    private final anc ʻ;
    private boolean ʻॱ;
    private final SparseArray<TsPayloadReader> ʼ;
    private final List<anq> ʽ;
    private int ʽॱ;
    private int ˈ;
    private final SparseBooleanArray ˊॱ;
    private final ags ˋॱ;
    private final int ˎ;
    private agv ˏॱ;
    private ady ͺ;
    private final SparseBooleanArray ॱˊ;
    private boolean ॱˋ;
    private TsPayloadReader ॱˎ;
    private final SparseIntArray ॱॱ;
    private int ॱᐝ;
    private final d ᐝ;
    private boolean ᐝॱ;

    class a implements agq {
        private final int ˊ;
        private final SparseArray<TsPayloadReader> ˋ = new SparseArray();
        private final SparseIntArray ˎ = new SparseIntArray();
        final /* synthetic */ TsExtractor ˏ;
        private final ane ॱ = new ane(new byte[5]);

        public a(TsExtractor tsExtractor, int i) {
            this.ˏ = tsExtractor;
            this.ˊ = i;
        }

        public void ˊ(anq o_anq, ady o_ady, com.google.android.exoplayer2.extractor.ts.TsPayloadReader.a aVar) {
        }

        public void ˏ(anc o_anc) {
            if (o_anc.ʽ() == 2) {
                anq o_anq;
                int ॱ;
                int ॱ2;
                if (this.ˏ.ˎ == 1 || this.ˏ.ˎ == 2 || this.ˏ.ॱᐝ == 1) {
                    o_anq = (anq) this.ˏ.ʽ.get(0);
                } else {
                    o_anq = new anq(((anq) this.ˏ.ʽ.get(0)).ˏ());
                    this.ˏ.ʽ.add(o_anq);
                }
                o_anc.ˎ(2);
                int ॱॱ = o_anc.ॱॱ();
                o_anc.ˎ(3);
                o_anc.ॱ(this.ॱ, 2);
                this.ॱ.ˎ(3);
                this.ˏ.ʽॱ = this.ॱ.ॱ(13);
                o_anc.ॱ(this.ॱ, 2);
                this.ॱ.ˎ(4);
                o_anc.ˎ(this.ॱ.ॱ(12));
                if (this.ˏ.ˎ == 2 && this.ˏ.ॱˎ == null) {
                    this.ˏ.ॱˎ = this.ˏ.ᐝ.ˎ(21, new b(21, null, null, anr.ʽ));
                    this.ˏ.ॱˎ.ˎ(o_anq, this.ˏ.ͺ, new com.google.android.exoplayer2.extractor.ts.TsPayloadReader.a(ॱॱ, 21, 8192));
                }
                this.ˋ.clear();
                this.ˎ.clear();
                int ˊ = o_anc.ˊ();
                while (ˊ > 0) {
                    o_anc.ॱ(this.ॱ, 5);
                    int ॱ3 = this.ॱ.ॱ(8);
                    this.ॱ.ˎ(3);
                    ॱ = this.ॱ.ॱ(13);
                    this.ॱ.ˎ(4);
                    ॱ2 = this.ॱ.ॱ(12);
                    b ˎ = ˎ(o_anc, ॱ2);
                    if (ॱ3 == 6) {
                        ॱ3 = ˎ.ˋ;
                    }
                    ॱ2 = ˊ - (ॱ2 + 5);
                    ˊ = this.ˏ.ˎ == 2 ? ॱ3 : ॱ;
                    if (this.ˏ.ˊॱ.get(ˊ)) {
                        ˊ = ॱ2;
                    } else {
                        Object ʻ;
                        if (this.ˏ.ˎ == 2 && ॱ3 == 21) {
                            ʻ = this.ˏ.ॱˎ;
                        } else {
                            ʻ = this.ˏ.ᐝ.ˎ(ॱ3, ˎ);
                        }
                        if (this.ˏ.ˎ != 2 || ॱ < this.ˎ.get(ˊ, 8192)) {
                            this.ˎ.put(ˊ, ॱ);
                            this.ˋ.put(ˊ, ʻ);
                        }
                        ˊ = ॱ2;
                    }
                }
                ॱ = this.ˎ.size();
                for (ˊ = 0; ˊ < ॱ; ˊ++) {
                    ॱ2 = this.ˎ.keyAt(ˊ);
                    int valueAt = this.ˎ.valueAt(ˊ);
                    this.ˏ.ˊॱ.put(ॱ2, true);
                    this.ˏ.ॱˊ.put(valueAt, true);
                    TsPayloadReader tsPayloadReader = (TsPayloadReader) this.ˋ.valueAt(ˊ);
                    if (tsPayloadReader != null) {
                        if (tsPayloadReader != this.ˏ.ॱˎ) {
                            tsPayloadReader.ˎ(o_anq, this.ˏ.ͺ, new com.google.android.exoplayer2.extractor.ts.TsPayloadReader.a(ॱॱ, ॱ2, 8192));
                        }
                        this.ˏ.ʼ.put(valueAt, tsPayloadReader);
                    }
                }
                if (this.ˏ.ˎ != 2) {
                    this.ˏ.ʼ.remove(this.ˊ);
                    this.ˏ.ॱᐝ = this.ˏ.ˎ == 1 ? 0 : this.ˏ.ॱᐝ - 1;
                    if (this.ˏ.ॱᐝ == 0) {
                        this.ˏ.ͺ.ˊ();
                        this.ˏ.ᐝॱ = true;
                    }
                } else if (!this.ˏ.ᐝॱ) {
                    this.ˏ.ͺ.ˊ();
                    this.ˏ.ॱᐝ = 0;
                    this.ˏ.ᐝॱ = true;
                }
            }
        }

        private b ˎ(anc o_anc, int i) {
            int ˋ = o_anc.ˋ();
            int i2 = ˋ + i;
            int i3 = -1;
            String str = null;
            List list = null;
            while (o_anc.ˋ() < i2) {
                int ʽ = o_anc.ʽ();
                int ʽ2 = o_anc.ʽ() + o_anc.ˋ();
                if (ʽ == 5) {
                    long ˋॱ = o_anc.ˋॱ();
                    if (ˋॱ == TsExtractor.ˊ) {
                        i3 = 129;
                    } else if (ˋॱ == TsExtractor.ॱ) {
                        i3 = 135;
                    } else if (ˋॱ == TsExtractor.ˋ) {
                        i3 = 36;
                    }
                } else if (ʽ == 106) {
                    i3 = 129;
                } else if (ʽ == 122) {
                    i3 = 135;
                } else if (ʽ == 123) {
                    i3 = 138;
                } else if (ʽ == 10) {
                    str = o_anc.ˊ(3).trim();
                } else if (ʽ == 89) {
                    i3 = 89;
                    list = new ArrayList();
                    while (o_anc.ˋ() < ʽ2) {
                        String trim = o_anc.ˊ(3).trim();
                        int ʽ3 = o_anc.ʽ();
                        byte[] bArr = new byte[4];
                        o_anc.ˎ(bArr, 0, 4);
                        list.add(new com.google.android.exoplayer2.extractor.ts.TsPayloadReader.c(trim, ʽ3, bArr));
                    }
                }
                o_anc.ˎ(ʽ2 - o_anc.ˋ());
            }
            o_anc.ˋ(i2);
            return new b(i3, str, list, Arrays.copyOfRange(o_anc.ˏ, ˋ, i2));
        }
    }

    class c implements agq {
        final /* synthetic */ TsExtractor ˎ;
        private final ane ˏ = new ane(new byte[4]);

        public c(TsExtractor tsExtractor) {
            this.ˎ = tsExtractor;
        }

        public void ˊ(anq o_anq, ady o_ady, com.google.android.exoplayer2.extractor.ts.TsPayloadReader.a aVar) {
        }

        public void ˏ(anc o_anc) {
            if (o_anc.ʽ() == 0) {
                o_anc.ˎ(7);
                int ˊ = o_anc.ˊ() / 4;
                for (int i = 0; i < ˊ; i++) {
                    o_anc.ॱ(this.ˏ, 4);
                    int ॱ = this.ˏ.ॱ(16);
                    this.ˏ.ˎ(3);
                    if (ॱ == 0) {
                        this.ˏ.ˎ(13);
                    } else {
                        ॱ = this.ˏ.ॱ(13);
                        this.ˎ.ʼ.put(ॱ, new ago(new a(this.ˎ, ॱ)));
                        this.ˎ.ॱᐝ = this.ˎ.ॱᐝ + 1;
                    }
                }
                if (this.ˎ.ˎ != 2) {
                    this.ˎ.ʼ.remove(0);
                }
            }
        }
    }

    public TsExtractor() {
        this(0);
    }

    public TsExtractor(int i) {
        this(1, i);
    }

    public TsExtractor(int i, int i2) {
        this(i, new anq(0), new DefaultTsPayloadReaderFactory(i2));
    }

    public TsExtractor(int i, anq o_anq, d dVar) {
        this.ᐝ = (d) amm.ॱ(dVar);
        this.ˎ = i;
        if (i == 1 || i == 2) {
            this.ʽ = Collections.singletonList(o_anq);
        } else {
            this.ʽ = new ArrayList();
            this.ʽ.add(o_anq);
        }
        this.ʻ = new anc(new byte[9400], 0);
        this.ˊॱ = new SparseBooleanArray();
        this.ॱˊ = new SparseBooleanArray();
        this.ʼ = new SparseArray();
        this.ॱॱ = new SparseIntArray();
        this.ˋॱ = new ags();
        this.ʽॱ = -1;
        ᐝ();
    }

    public boolean ˎ(adv o_adv) throws IOException, InterruptedException {
        byte[] bArr = this.ʻ.ˏ;
        o_adv.ˎ(bArr, 0, 940);
        for (int i = 0; i < 188; i++) {
            for (int i2 = 0; i2 < 5; i2++) {
                if (bArr[(i2 * 188) + i] != (byte) 71) {
                    i2 = 0;
                    break;
                }
            }
            boolean z = true;
            if (i2 != 0) {
                o_adv.ॱ(i);
                return true;
            }
        }
        return false;
    }

    public void ˏ(ady o_ady) {
        this.ͺ = o_ady;
    }

    public void ˊ(long j, long j2) {
        amm.ˏ(this.ˎ != 2);
        int size = this.ʽ.size();
        for (int i = 0; i < size; i++) {
            anq o_anq = (anq) this.ʽ.get(i);
            if (!((o_anq.ˎ() == -9223372036854775807L ? 1 : null) == null && (o_anq.ˎ() == 0 || o_anq.ˏ() == j2))) {
                o_anq.ॱ();
                o_anq.ॱ(j2);
            }
        }
        if (!(j2 == 0 || this.ˏॱ == null)) {
            this.ˏॱ.ˏ(j2);
        }
        this.ʻ.ˏ();
        this.ॱॱ.clear();
        for (int i2 = 0; i2 < this.ʼ.size(); i2++) {
            ((TsPayloadReader) this.ʼ.valueAt(i2)).ˏ();
        }
        this.ˈ = 0;
    }

    public void ॱ() {
    }

    public int ˎ(adv o_adv, adz o_adz) throws IOException, InterruptedException {
        long ॱ = o_adv.ॱ();
        if (this.ᐝॱ) {
            Object obj = (ॱ == -1 || this.ˎ == 2) ? null : 1;
            if (obj != null && !this.ˋॱ.ˋ()) {
                return this.ˋॱ.ˏ(o_adv, o_adz, this.ʽॱ);
            }
            ˋ(ॱ);
            if (this.ʻॱ) {
                this.ʻॱ = false;
                ˊ(0, 0);
                if (o_adv.ˊ() != 0) {
                    o_adz.ˊ = 0;
                    return 1;
                }
            }
            if (this.ˏॱ != null && this.ˏॱ.ॱ()) {
                return this.ˏॱ.ˋ(o_adv, o_adz, null);
            }
        }
        if (!ˊ(o_adv)) {
            return -1;
        }
        int ॱॱ = ॱॱ();
        int ॱ2 = this.ʻ.ॱ();
        if (ॱॱ > ॱ2) {
            return 0;
        }
        int ॱᐝ = this.ʻ.ॱᐝ();
        if ((8388608 & ॱᐝ) != 0) {
            this.ʻ.ˋ(ॱॱ);
            return 0;
        }
        int i = 0 | ((4194304 & ॱᐝ) != 0 ? 1 : 0);
        int i2 = (2096896 & ॱᐝ) >> 8;
        Object obj2 = (ॱᐝ & 32) != 0 ? 1 : null;
        TsPayloadReader tsPayloadReader = ((ॱᐝ & 16) != 0 ? 1 : null) != null ? (TsPayloadReader) this.ʼ.get(i2) : null;
        if (tsPayloadReader == null) {
            this.ʻ.ˋ(ॱॱ);
            return 0;
        }
        int i3;
        if (this.ˎ != 2) {
            i3 = ॱᐝ & 15;
            ॱᐝ = this.ॱॱ.get(i2, i3 - 1);
            this.ॱॱ.put(i2, i3);
            if (ॱᐝ == i3) {
                this.ʻ.ˋ(ॱॱ);
                return 0;
            } else if (i3 != ((ॱᐝ + 1) & 15)) {
                tsPayloadReader.ˏ();
            }
        }
        if (obj2 != null) {
            i3 = ((this.ʻ.ʽ() & 64) != 0 ? 2 : 0) | i;
            this.ʻ.ˎ(this.ʻ.ʽ() - 1);
        } else {
            i3 = i;
        }
        boolean z = this.ᐝॱ;
        if (ˊ(i2)) {
            this.ʻ.ॱ(ॱॱ);
            tsPayloadReader.ॱ(this.ʻ, i3);
            this.ʻ.ॱ(ॱ2);
        }
        if (!(this.ˎ == 2 || z || !this.ᐝॱ || ॱ == -1)) {
            this.ʻॱ = true;
        }
        this.ʻ.ˋ(ॱॱ);
        return 0;
    }

    private void ˋ(long j) {
        if (!this.ॱˋ) {
            this.ॱˋ = true;
            if (this.ˋॱ.ˏ() != -9223372036854775807L) {
                this.ˏॱ = new agv(this.ˋॱ.ॱ(), this.ˋॱ.ˏ(), j, this.ʽॱ);
                this.ͺ.ˎ(this.ˏॱ.ˋ());
                return;
            }
            this.ͺ.ˎ(new e(this.ˋॱ.ˏ()));
        }
    }

    private boolean ˊ(adv o_adv) throws IOException, InterruptedException {
        int ˊ;
        byte[] bArr = this.ʻ.ˏ;
        if (9400 - this.ʻ.ˋ() < 188) {
            ˊ = this.ʻ.ˊ();
            if (ˊ > 0) {
                System.arraycopy(bArr, this.ʻ.ˋ(), bArr, 0, ˊ);
            }
            this.ʻ.ॱ(bArr, ˊ);
        }
        while (this.ʻ.ˊ() < 188) {
            ˊ = this.ʻ.ॱ();
            int ˊ2 = o_adv.ˊ(bArr, ˊ, 9400 - ˊ);
            if (ˊ2 == -1) {
                return false;
            }
            this.ʻ.ॱ(ˊ + ˊ2);
        }
        return true;
    }

    private int ॱॱ() throws ParserException {
        int ˋ = this.ʻ.ˋ();
        int ॱ = this.ʻ.ॱ();
        int ॱ2 = aha.ॱ(this.ʻ.ˏ, ˋ, ॱ);
        this.ʻ.ˋ(ॱ2);
        int i = ॱ2 + 188;
        if (i > ॱ) {
            this.ˈ = (ॱ2 - ˋ) + this.ˈ;
            if (this.ˎ == 2 && this.ˈ > 376) {
                throw new ParserException("Cannot find sync byte. Most likely not a Transport Stream.");
            }
        }
        this.ˈ = 0;
        return i;
    }

    private boolean ˊ(int i) {
        if (this.ˎ == 2 || this.ᐝॱ || !this.ॱˊ.get(i, false)) {
            return true;
        }
        return false;
    }

    private void ᐝ() {
        this.ˊॱ.clear();
        this.ʼ.clear();
        SparseArray ॱ = this.ᐝ.ॱ();
        int size = ॱ.size();
        for (int i = 0; i < size; i++) {
            this.ʼ.put(ॱ.keyAt(i), ॱ.valueAt(i));
        }
        this.ʼ.put(0, new ago(new c(this)));
        this.ॱˎ = null;
    }
}
